/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.usercenter.application.das;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.usercenter.common.enums.DeptStatusEnum;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.usercenter.application.bo.DeptBO;
import cn.hallele.ms.usercenter.domain.action.dept.DeptDomainService;
import cn.hallele.ms.usercenter.domain.domain.DeptDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * hallele_ms_usercenter
 *
 * @author anle5
 * @since 2024/11/16 16:18
 */
@Slf4j
@Component
public class DeptBDDas extends ModelDas<DeptDomainService, DeptBO, DeptDomain> {
    protected DeptBDDas(DeptDomainService option) {
        super(option);
    }

    /**
     * 分页查询所有部门名
     *
     * @param pageNum   页码
     * @param pageSize  每页数量
     * @param condition 查询条件
     * @return 分页结果
     */
    public Page<DeptBO> findAll(int pageNum, int pageSize, DeptBO condition) {
        return revertPage(this.option.findAll(pageNum, pageSize, convert(condition)));
    }

    /**
     * 查询单个部门信息
     *
     * @param condition 查询条件
     * @return 部门信息
     */
    public DeptBO findOne(DeptBO condition) {
        return revert(this.option.findOne(convert(condition)));
    }

    /**
     * 新增 部门
     *
     * @param deptBO 部门
     */
    public void addOne(DeptBO deptBO) {
        this.option.addOne(convert(deptBO));
    }

    /**
     * 更新 部门
     *
     * @param deptBO 部门
     */
    public void updateOne(DeptBO deptBO) {
        this.option.updateOne(convert(deptBO));
    }

    /**
     * 删除 部门
     *
     * @param id 要删除的dept id
     */
    public void delete(Integer id) {
        this.option.delete(id);
    }

    /**
     * 更新 部门 状态
     *
     * @param id         主键
     * @param deptStatus 状态
     */
    public void updateDeptStatus(Integer id, DeptStatusEnum deptStatus) {
        this.option.updateDeptStatus(id, deptStatus);
    }

    /**
     * 将T转成E
     *
     * @param deptBO 被转换者
     * @return S 转换后者
     */
    @Override
    protected DeptDomain convert(DeptBO deptBO) {
        return ObjectUtil.map(deptBO, d -> DeptDomain.builder()
                .id(d.getId())
                .number(d.getNumber())
                .name(d.getName())
                .description(d.getDescription())
                .leader(ObjectUtil.map(d.getLeader(), DeptBO.Leader::getId))
                .phone(d.getPhone())
                .email(d.getEmail())
                .status(d.getStatus())
                .sorted(d.getSorted())
                .parentId(d.getParentId())
                .createTime(d.getCreateTime())
                .updateTime(d.getUpdateTime())
                .createBy(d.getCreateBy())
                .updateBy(d.getUpdateBy())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param deptDomain 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected DeptBO revert(DeptDomain deptDomain) {
        return ObjectUtil.map(deptDomain, d -> DeptBO.builder()
                .id(d.getId())
                .number(d.getNumber())
                .name(d.getName())
                .description(d.getDescription())
                .leader(DeptBO.Leader.builder()
                        .id(d.getLeader())
                        .build())
                .phone(d.getPhone())
                .email(d.getEmail())
                .status(d.getStatus())
                .sorted(d.getSorted())
                .parentId(d.getParentId())
                .createTime(d.getCreateTime())
                .updateTime(d.getUpdateTime())
                .createBy(d.getCreateBy())
                .updateBy(d.getUpdateBy())
                .build());
    }
}
